package com.blue.base.oauth.server.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.common.util.OAuth2Utils;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.security.Principal;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 替代默认的授权认证允许页面
 * 有兴趣可以把这个类的代码全部注释掉，会发现spring security oauth2.0会自动启用默认的权限授权html页面，我们的access_confirm.html也是仿照内置的页面去实现的
 *
 * @author liulei
 * @version 1.0
 */
@Slf4j
@Controller
@SessionAttributes("authorizationRequest")//对应AuthorizationEndpoint.AUTHORIZATION_REQUEST_ATTR_NAME
public class ConfirmAccessController {

    @RequestMapping("/oauth/confirm_access")
    public String getAccessConfirmation(Map<String, Object> model, Principal principal) {
        AuthorizationRequest clientAuth = (AuthorizationRequest) model.remove("authorizationRequest");
        model.put("clientId", clientAuth.getClientId());
        Map<String, String> scopes = new LinkedHashMap<>();
        scopes.put(OAuth2Utils.SCOPE_PREFIX + clientAuth.getScope(), "true");
        log.info("授权页面获取客户端信息：{}", clientAuth.getClientId());
        log.info("授权页面获取用户信息：{}", principal.getName());
        return "access_confirm";
    }
}
